package jp.eisbahn.mgapi.api.people;

import java.io.InputStream;
import java.util.Map;

import jp.eisbahn.mgapi.api.AbstractPagingRequest;
import jp.eisbahn.mgapi.api.Response;
import jp.eisbahn.mgapi.http.ContentType;
import jp.eisbahn.mgapi.http.HttpMethod;
import jp.eisbahn.mgapi.http.HttpUtils;
import jp.eisbahn.mgapi.util.StringUtils;

/**
 * The request class to retrieve the person information with People API.
 * @author Yoichiro Tanaka
 */
public class PeopleRequest extends AbstractPagingRequest {

    /** The base URI string. */
    private static final String BASE_URI = "/people/{userId}/{groupId}";

    /** The user ID. */
    private String userId;

    /** The group ID. */
    private String groupId;

    /** The sort by. */
    private String sortBy;

    /** The sort order. */
    private SortOrder sortOrder;

    /** The fields. */
    private String[] fields;

    /**
     * Retrieve the user ID.
     * @return The user ID string.
     */
    public final String getUserId() {
        return userId;
    }

    /**
     * Set the user ID.
     * @param theUserId The user ID string.
     */
    public final void setUserId(final String theUserId) {
        userId = theUserId;
    }

    /**
     * Retrieve the group ID.
     * @return The group ID string.
     */
    public final String getGroupId() {
        return groupId;
    }

    /**
     * Set the new group ID.
     * @param newGroupId The new group ID string.
     */
    public final void setGroupId(final String newGroupId) {
        groupId = newGroupId;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getUri()
     */
    @Override
    public final String getUri() {
        String[] sortByParam = null;
        if (sortBy != null) {
            sortByParam = new String[] {"sortBy", sortBy};
        }
        String[] sortOrderParam = null;
        if (sortOrder != null) {
            sortOrderParam = new String[] {"sortOrder", sortOrder.toString()};
        }
        String[] fieldsParam = null;
        if (fields != null && fields.length > 0) {
            fieldsParam = new String[] {
                    "fields", StringUtils.join(fields, ",")};
        }
        String query = HttpUtils.createQueryParameterString(
                getStartIndexQueryParameter(),
                getCountQueryParameter(),
                sortByParam,
                sortOrderParam,
                fieldsParam
        );
        return BASE_URI.replace("{userId}", userId)
                       .replace("{groupId}", groupId)
                       + "?" + query;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getHttpMethod()
     */
    @Override
    public final HttpMethod getHttpMethod() {
        return HttpMethod.GET;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getResponseClass()
     */
    @Override
    public final Class<? extends Response> getResponseClass() {
        return PeopleResponse.class;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getContentType()
     */
    @Override
    public final ContentType getContentType() {
        return ContentType.X_WWW_FORM_URLENCODED;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getBodyStream()
     */
    @Override
    public final InputStream getBodyStream() {
        return null;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getParameterMap()
     */
    @Override
    public final Map<String, Object> getParameterMap() {
        return null;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getHeaderMap()
     */
    @Override
    public final Map<String, String> getHeaderMap() {
        return null;
    }

    /**
     * Retrieve the sortBy value.
     * @return The sortBy value.
     */
    public final String getSortBy() {
        return sortBy;
    }

    /**
     * Set a sortBy value.
     * @param newSortBy The sortBy to set
     */
    public final void setSortBy(final String newSortBy) {
        this.sortBy = newSortBy;
    }

    /**
     * Retrieve the sortOrder value.
     * @return The sortOrder value.
     */
    public final SortOrder getSortOrder() {
        return sortOrder;
    }

    /**
     * Set a sortOrder value.
     * @param newSortOrder The sortOrder to set
     */
    public final void setSortOrder(final SortOrder newSortOrder) {
        this.sortOrder = newSortOrder;
    }

    /**
     * Retrieve the fields value.
     * @return The fields value.
     */
    public final String[] getFields() {
        return fields;
    }

    /**
     * Set a fields value.
     * @param newFields The fields to set
     */
    public final void setFields(final String[] newFields) {
        this.fields = newFields;
    }

}
